<template>
  <view class="goods_list">
    <view class="search-input">
      <search-input></search-input>
    </view>
    <view class="navbar-warp">
      <nav-bar :list="tabList" :activeIndex="activeIndex"
        @tabsItemChange="tabsItemChange"></nav-bar>
    </view>
    <view class="content">
      <view class="goods_item" v-for="item in goodsList" :key="item.goods_id"
        @click="clickListItem(item.goods_id)">
        <view class="goods_img_wrap">
          <image mode="widthFix" :src="
              item.goods_small_logo
                ? item.goods_small_logo
                : 'https://ww1.sinaimg.cn/large/007rAy9hgy1g24by9t530j30i20i2glm.jpg'
            "></image>
        </view>
        <view class="goods_info_wrap">
          <view class="goods_name">{{ item.goods_name }}</view>
          <view class="goods_price">￥{{ item.goods_price }}</view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
  import {
    showToast
  } from "../../utils/util"
  export default {
    data() {
      return {
        goodsList: [],
        activeIndex: 0,
        tabList: [{
            id: 0,
            name: "综合",
          },
          {
            id: 1,
            name: "销量",
          },
          {
            id: 2,
            name: "价格",
          },
        ],
        cid: "",
        query: "",
        pagenum: 1,
        pagesize: 20,
        hasMore: true,
      }
    },
    onLoad(options) {
      if (options.cid) {
        this.cid = options.cid
      } else {
        this.query = options.query
      }

      this.getGoodsList()
    },
    onReachBottom() {
      this.getGoodsList()
    },
    methods: {
      clickListItem(goods_id) {
        console.log(goods_id)
        uni.navigateTo({
          url: "/pages/goods_detail/goods_detail?goods_id=" + goods_id,
        })
      },
      getGoodsList() {
        if (!this.hasMore) {
          showToast({
            title: "没有更多了哟",
          })
          return
        }
        let data = {}
        if (this.cid) {
          data.cid = this.cid
        } else {
          data.query = this.query
        }
        data.pagenum = this.pagenum
        data.pagesize = this.pagesize
        this.$http({
          url: "/goods/search",
          method: "GET",
          data,
        }).then((res) => {
          this.goodsList = [...this.goodsList, ...res.goods]
          this.pagenum = this.pagenum + 1
          if (res.goods.length < this.pagesize) {
            this.hasMore = false
          }
        })
      },
      tabsItemChange(e) {
        this.activeIndex = e
      },
    },
  }
</script>

<style lang="scss">
  .goods_list {
    width: 100vw;
    height: 100vh;

    .search-input {
      width: 100%;
      height: 90upx;
    }

    .navbar-warp {
      width: 100%;
      height: 80upx;
    }

    .content {
      .goods_item {
        display: flex;
        border-bottom: 2upx solid #ccc;
        height: 224upx;
        width: 100%;

        .goods_img_wrap {
          flex: 2;
          height: 100%;
          display: flex;
          justify-content: center;
          align-items: center;

          image {
            width: 70% !important;
            height: 70% !important;
          }
        }

        .goods_info_wrap {
          flex: 3;
          display: flex;
          flex-direction: column;
          justify-content: space-around;
          padding-right: 30upx;

          .goods_name {
            display: -webkit-box;
            overflow: hidden;
            -webkit-box-orient: vertical;
            -webkit-line-clamp: 2;
          }

          .goods_price {
            color: #eb4450;
            font-size: 32upx;
          }
        }
      }
    }
  }
</style>
